//
//  STLineViewController.swift
//  iPhone
//
//  Created by Toj on 11/2/22.
//

import UIKit

// MARK: - 懒加载(属性), 变量, 初始化init
class STLineViewController: STBaseViewController {
    
    private let lineWidth = 40.to_cgfloat
    private let lineHeight = 30.to_cgfloat
    private let space = 0.to_cgfloat
    private let hheight = 20.to_cgfloat
    
    private lazy var lineView: UIImageView = {
        let view = UIImageView()
        let size     = CGSize(width: .sr_width, height: hheight)
        let lineSize = CGSize(width: lineWidth, height: lineHeight)
        
        view.frame = CGRect(origin: .zero, size: size)
        view.image = dottedLine(size: size, color: .black, lineSize: lineSize, space: space)
        return view
    }()
    
    private lazy var lineView2: UIImageView = {
        let view = UIImageView()
        let size     = CGSize(width: .sr_width, height: hheight)
        let lineSize = CGSize(width: lineWidth, height: lineHeight)
        
        view.frame = CGRect(origin: .zero, size: size)
        let image = UIImage.st_dottedLine(size: size, color: .black, lineSize: lineSize, space: space)
        view.image = image
        
        return view
    }()
    
    private lazy var lineView3: UIImageView = {
        let view = UIImageView()
        let size     = CGSize(width: .sr_width, height: hheight)
        let lineSize = CGSize(width: lineWidth, height: lineHeight)
        
        view.frame = CGRect(origin: .zero, size: size)
        view.st_dottedLine(color: .black, lineSize: lineSize, space: space)
        
        return view
    }()
}

// MARK: - 生命周期
extension STLineViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
        setupMakeInitialize()
    }
}

// MARK: - Private
private extension STLineViewController {
    
    /* Test */
    func dottedLine(size: CGSize, color: UIColor, lineSize: CGSize, space: CGFloat = 0) -> UIImage? {
        guard size != .zero else { return nil }
        
        let lWidth = lineSize.width
        let lHeight = lineSize.height
        let cHeight = size.height
        
        let lineWidth = lWidth >= 1 ? lWidth : 1
        let lineHeight = fmin(lHeight, cHeight)
        let lineSpace = space > 0 ? space : lineWidth
        let fixHeight = lineHeight <= 1 ? lineHeight : 0
        let lineStartY = (cHeight - fixHeight) * 0.5
        
        let image = UIImageContext(size: size) { context in
            /// 设置参数
            // 线条终点的形状
            context?.setLineCap(.butt)
            // 虚线颜色
            context?.setStrokeColor(color.cgColor)
            // 虚线高度
            context?.setLineWidth(lineHeight)
            // phase: 起始位置
            // lengths:
            // 单: 虚线长度, 双: 间距长度. 只设置一个值虚线间距等长
            let lengths = [lineWidth, lineSpace]
            context?.setLineDash(phase: 0, lengths: lengths)
            
            /// 绘制
            // 设置虚线绘制起点
            let startPoint = CGPoint(x: 0, y: lineStartY)
            context?.move(to: startPoint)
            // 设置虚线绘制终点
            let endPoint = CGPoint(x: size.width, y: lineStartY)
            context?.addLine(to: endPoint)
            context?.strokePath()
        }
        return image
    }
}

// MARK: - 初始化设置
private extension STLineViewController {
    
    func setupMakeAddSubviews() {
        
        view.addSubview(lineView)
        view.addSubview(lineView2)
        view.addSubview(lineView3)
    }
    
    func setupMakeLayoutSubviews() {
        
        var size = lineView.frame.size
        lineView.snp.remakeConstraints { make in
            make.size.equalTo(size)
            make.center.equalTo(view)
        }
        
        size = lineView.frame.size
        lineView2.snp.remakeConstraints { make in
            make.size.equalTo(size)
            make.centerX.equalTo(view)
            make.top.equalTo(lineView.snp.bottom)
        }
        
        size = lineView3.frame.size
        lineView3.snp.remakeConstraints { make in
            make.centerX.equalTo(view)
            make.size.equalTo(size)
            make.top.equalTo(lineView2.snp.bottom)
        }
        
        let random = UIColor.random
        let random2 = UIColor.random
        var x = 0.to_cgfloat
        var i = 0
        while x < .sr_width {
            let isSingle = (i % 2 == 0)
            let lineV = UIView()
            lineV.backgroundColor = isSingle ? random : random2
            let lwidth = space > 0 ? space : lineWidth
            view.addSubview(lineV)
            lineV.snp.remakeConstraints { make in
                make.width.equalTo(lwidth)
                make.height.equalTo(lineHeight)
                make.left.equalTo(x)
                make.bottom.equalTo(lineView.snp.top)
            }
            
            x += lwidth
            i += 1
        }
    }
    
    func setupMakeInitialize() {
        view.backgroundColor = .random
        
        lineView.backgroundColor = lineView.superview?.backgroundColor
        
        // lineView.backgroundColor  = .random
        lineView2.backgroundColor = .random
        lineView3.backgroundColor = .random
    }
}
